package com.nulldev.util.data.Arrays.misc;

import java.util.Collection;
import java.util.concurrent.ConcurrentLinkedDeque;

import com.nulldev.util.concurrency.threadIt.v4.locking.SignalBarrier;

public class LockableConcurrentLinkedDeque<E> extends ConcurrentLinkedDeque<E> {

	private static final long serialVersionUID = 4265380385428350741L;
	private final SignalBarrier newItemBarr = new SignalBarrier();

	public LockableConcurrentLinkedDeque() {
		super();
	}

	public LockableConcurrentLinkedDeque(final Collection<? extends E> c) {
		super(c);
	}

	public E take() throws InterruptedException {
		if (this.isEmpty())
			this.newItemBarr.await();
		return super.poll();
	}

	@Override
	public boolean add(E e) {
		this.newItemBarr.signal();
		return super.add(e);
	}

	@Override
	public void addFirst(E e) {
		this.newItemBarr.signal();
		super.addFirst(e);
	}

	@Override
	public void addLast(E e) {
		this.newItemBarr.signal();
		super.addLast(e);
	}

	@Override
	public boolean offer(E e) {
		this.newItemBarr.signal();
		return super.offer(e);
	}

	@Override
	public boolean offerFirst(E e) {
		this.newItemBarr.signal();
		return super.offerFirst(e);
	}

	@Override
	public boolean offerLast(E e) {
		this.newItemBarr.signal();
		return super.offerLast(e);
	}

	@Override
	public E poll() {
		if (!this.isEmpty())
			this.newItemBarr.signal();
		return super.poll();
	}
}
